<?php

/**
 * alltosun.com 转移文件 transfer.php
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 宁海新 (ninghx@alltosun.com) $
 * $Date: 2012-4-11 下午04:59:12 $
 * $Id: transfer.php 4386 2012-04-12 05:02:59Z liw $
*/

class Action
{
    private $uh = 'uchome_';
    private $uc = 'uc_';
    private $db = null;
    private $db_transfer = null;
    private $perpage = 500;
    private $user_id_relation;

    public function __construct()
    {
        // 获得数据
        $this->db_transfer  = DB::connect(Config::Get('db_transfer'));
        // 存入数据
        $this->db = DB::connect(Config::Get('db'));
        $this->user_id_relation = Config::get('user_id_relation');
    }

    public function __call($action = '', $params = array())
    {

    }

    //用户表
    public function user()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $member_list = $this->db_transfer->getList($this->uh.'member', array(1=>1), " $limit");

        while(!empty($member_list)) {
            foreach ($member_list as $k=>$v) {
                $spacefield_info = $this->db_transfer->read($this->uh.'spacefield', array('uid'=>$v['uid']));
                $space_info = $this->db_transfer->read($this->uh.'space', array('uid'=>$v['uid']));
                $space_info = $this->db_transfer->read($this->uh.'space', array('uid'=>$v['uid']));
                $member_info = array(
                    'id'              => $this->_modify_user_id($v['uid']),
                    'user_name'       => $v['username'],
                   // 'password'         => $v['password'],
                   'password' => md5('alltosun'),
                    'url'                    => empty($space_info['domain']) ? '' : $space_info['domain'],
                    'email'	             => empty($spacefield_info['email']) ? '' : $spacefield_info['email'],
                    'hash'				 => user_helper::random_hash(),
                    'add_time'         => empty($space_info['dateline']) ? date("Y-m-d H:i:s") : date("Y-m-d H:i:s", $space_info['dateline']),
                    'update_time'    => empty($space_info['updatetime']) ? date("Y-m-d H:i:s") : date("Y-m-d H:i:s", $space_info['updatetime']),
                    'last_login_time' => empty($space_info['lastlogin']) ? 0 : date("Y-m-d H:i:s", $space_info['lastlogin']),
                );
                $this->db->create('user', $member_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $member_list = $this->db_transfer->getList($this->uh.'member', array(1=>1), " $limit");
        }
    }

     //日志分类
    public function catalog()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $class_list = $this->db_transfer->getList($this->uh.'class', array(1=>1), " $limit");

        while(!empty($class_list)) {
            foreach ($class_list as $k=>$v) {
                $class_info = array(
                    'id'         => $v['classid'],
                    'name'       => $v['classname'],
                    'user_id'    => $v['uid'],
                    'res_name'   => 'blog',
                );

                $class_info['user_id'] = $this->_modify_user_id($v['uid']);
                $this->db->create('catalog', $class_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $class_list = $this->db_transfer->getList($this->uh.'class', array(1=>1), " $limit");
        }
    }

    //日志内容
    public function blog()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $blog_list = $this->db_transfer->getList($this->uh.'blog', array(1=>1), " $limit");

        while(!empty($blog_list)) {
            foreach ($blog_list as $k=>$v) {
                $blogfield_list = $this->db_transfer->read($this->uh.'blogfield', array('blogid'=>$v['blogid']), " $limit");

                if (intval($v['friend']) == 0) {
                    $privacy = 1;
                } elseif (intval($v['friend']) == 1) {
                    $privacy = 2;
                } else {
                    $privacy = 0;
                }

                $blog_info = array(
                    'id'                 => $v['blogid'],
                    'user_id'         => $v['uid'],
                    'title'              => $v['subject'],
                    'catalog_id'    => $v['classid'],
                    'publish_time' => date("Y-m-d H:i:s", $v['dateline']),
                    'cover'           => $v['pic'],
                    'privacy'         => $privacy,
                    'cover'           => $v['pic'],
                    'content'        => empty($blogfield_list['message']) ? '' : $blogfield_list['message'],
                    'add_time'      => empty($blogfield_list['relatedtme']) ? date("Y=m-d H:i:s") : $blogfield_list['relatetme'],
                );
                $blog_info['user_id'] = $this->_modify_user_id($v['uid']);
                $this->db->create('blog', $blog_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $blog_list = $this->db_transfer->getList($this->uh.'blog', array(1=>1), " $limit");
        }
    }

    //日志回复
    public function comment()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $post_list = $this->db_transfer->getList($this->uh.'comment', array(1=>1), " $limit");

        while(!empty($post_list)) {
            foreach ($post_list as $k=>$v) {
                switch ($v['idtype']) {
                    case 'blogid':
                        $res_name = 'blog';
                        break;
                    case 'picid':
                        $res_name = 'photo';
                        break;
                    case 'uid':
                        $res_name = 'user';
                        break;
                    case 'sid':
                        continue;
                        $res_name = 'share';
                        break;
                }
                $post_info = array(
                    'id'            => $v['cid'],
                    'res_id'      => $v['id'],
                    'res_name' => $res_name,
                    'user_id'    => $this->_modify_user_id($v['authorid']),
                    'to_user_id' => $this->_modify_user_id($v['uid']),
                    'add_time' => date("Y-m-d H:i:s",$v['dateline']),
                    'content'   => $v['message'],
                );
                if ($v['idtype'] == 'uid') {
                    $post_info['is_message'] = 1;
                    $post_info['res_id'] = $this->_modify_user_id($v['id']);
                }

                $this->db->create('comment', $post_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $post_list = $this->db_transfer->getList($this->uh.'post', array(1=>1), " $limit");
        }
    }

     //相册
    public function album()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $album_list = $this->db_transfer->getList($this->uh.'album', array(1=>1), " $limit");

        while(!empty($album_list)) {
            foreach ($album_list as $k=>$v) {

                if ($v['friend'] == 0) {
                    $privacy = 1;
                } elseif ($v['friend'] == 1) {
                    $privacy = 2;
                } else {
                    $privacy = 0;
                }

                $album_info = array(
                    'id'                 => $v['albumid'],
                    'title'              => $v['albumname'],
                    'user_id'         => $v['uid'],
                    'add_time'      => date("Y-m-d, H:i:s", $v['dateline']),
                    'update_time' => date("Y-m-d H:i:s", $v['updatetime']),
                    'path'             => '/'.substr($v['pic'], 0, -10),
                    'privacy'         => $privacy,
                );

                $album_info['user_id'] = $this->_modify_user_id($album_info['user_id']);
                $this->db->create('album', $album_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $album_list = $this->db_transfer->getList($this->uh.'album', array(1=>1), " $limit");
        }
    }

    //图片
    public function pic()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $pic_list = $this->db_transfer->getList($this->uh.'pic', array(1=>1), " $limit");

        while(!empty($pic_list)) {
            foreach ($pic_list as $k=>$v) {
                $pic_info = array(
                    'id'            => $v['picid'],
                    'album_id' => $v['albumid'],
                    'user_id'    => $v['uid'],
                    'add_time' => date("Y-m-d H:i:s", $v['dateline']),
                    'title'         => $v['filename'],
                    'intro'       => $v['title'],
                    'path'       => '/'.$v['filepath'],
                );
                $pic_info['user_id'] = $this->_modify_user_id($pic_info['user_id']);
                $this->db->create('photo', $pic_info);

                // 生成缩略图
                if (trim($pic_info['path'], '/')) {
                    //make_thumb($pic_info['path'], 'photo');
                }
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $pic_list = $this->db_transfer->getList($this->uh.'pic', array(1=>1), " $limit");
        }
    }

    public function blog_view()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $blog_list = $this->db_transfer->getList($this->uh.'blog', array(1=>1), " $limit");

        while(!empty($blog_list)) {
            foreach ($blog_list as $k=>$v) {
                $blog_info = array(
                    'res_id'       => $v['blogid'],
                    'res_name' => 'blog',
                    'count'      => $v['viewnum'],
                );
                $this->db->create('view', $blog_info);
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $blog_list = $this->db_transfer->getList($this->uh.'blog', array(1=>1), " $limit");
        }
    }

    public function friend()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
        $friend_list = $this->db_transfer->getList($this->uh.'friend', array(1=>1), " $limit");

        while(!empty($friend_list)) {
            foreach ($friend_list as $k=>$v) {
                $is_group = empty($v['gid']) ? 0 : 1;
                $friend_info = array(
                    'user_id'       => $this->_modify_user_id($v['uid']),
                    'friend_id'     => $this->_modify_user_id($v['fuid']),
                    'is_group'     => $is_group,
                    'description' => $v['note'],
                    'add_time'    => date("Y-m-d H:i:s", $v['dateline']),
                );
                $this->db->create('friend', $friend_info);

                if ($is_group) {
                    $friend_group_relation_info = array(
                        'user_id'     => $this->_modify_user_id($v['uid']),
                        'friend_id ' => $this->_modify_user_id($v['fuid']),
                        'group_id'  => $v['gid'],
                    );
                    $this->db->create('friend_group_relation', $friend_group_relation_info);
                }
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * $this->perpage) . ", $this->perpage";
            $friend_list = $this->db_transfer->getList($this->uh.'friend', array(1=>1), " $limit");
        }
    }

    /**
     * 用员工生成账号
     */
    public function generate_staff()
    {
        $other_user_relation = Config::get('other_user_relation');
        foreach ($other_user_relation as $k=>$v) {
            $member_info = array(
                'id'              => $k,
                'user_name'       => $v,
               // 'password'         => $v['password'],
                'password' => md5('alltosun'),
                'url'             => '',
                'email'	          => '',
                'hash'		      => user_helper::random_hash(),
                'add_time'        => date('Y-m-d H:i:s'),
                'update_time'     => date('Y-m-d H:i:s'),
            );
            $this->db->create('user', $member_info);
        }
    }

    /**
     * 取得真实的user_id
     * Enter description here ...
     * @param unknown_type $user_id
     */
    public function _modify_user_id($user_id)
    {
        if (!$user_id) {
            return $user_id;
        }

        if (array_key_exists($user_id, $this->user_id_relation)) {
            $user_id = $this->user_id_relation[$user_id];
        } else {
            $user_id = $user_id + 100000;
        }
        return $user_id;
    }

    /**
     * 生成照片缩略图
     */
    public function generate_photo_thumb()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * 50) . ", 50";
        $photo_list = $this->db->getList('photo', array(1=>1), " $limit");

        while(!empty($photo_list)) {
            foreach ($photo_list as $k=>$v) {
                if (trim($v['path'], '/')) {
                    $new_path = '/uch_attachment'.$v['path'];
                    $info = array('path'=>$new_path);
                    _model('photo')->update($v['id'], $info);

                    make_thumb($new_path, 'photo');
                }
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * 50) . ", 50";
            $photo_list = $this->db->getList('photo', array(1=>1), " $limit");
        }
    }

    public function generate_album_thumb()
    {
        $i = 0;
        $limit = "LIMIT " . ($i * 50) . ", 50";
        $photo_list = $this->db->getList('album', array(1=>1), " $limit");

        while(!empty($photo_list)) {
            foreach ($photo_list as $k=>$v) {
                if (trim($v['path'], '/')) {
                    $new_path = '/uch_attachment'.$v['path'];
                    $info = array('path'=>$new_path);
                    _model('album')->update($v['id'], $info);

                }
            }

            usleep(500);

            $i++;
            $limit = "LIMIT " . ($i * 50) . ", 50";
            $photo_list = $this->db->getList('album', array(1=>1), " $limit");
        }
    }



    public function add_friends()
    {
        $user_list = _model('user')->getList(array(1=>1));
        foreach ($user_list as $k=>$v) {
            foreach ($user_list as $k1=>$v1) {
                if ($v['id'] != $v1['id']) {
                    $arr = array(
                        'user_id' => $v['id'],
                        'friend_id' => $v1['id']
                    );
                    _model('friend')->create($arr);
                }
            }
        }
    }

    public function add_message_unread()
    {
        $user_list = _model('user')->getList(array(1=>1));
        foreach ($user_list as $k=>$v) {
            $arr = array(
                'user_id' =>$v['id']
            );
            _model('message_unread')->create($arr);
        }
    }
}
?>